package com.ebuildy
{
	import flash.geom.Point;
	import flash.system.Security;
	import flash.utils.describeType;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.formatters.NumberFormatter;
	
	public class EbuildyUtils
	{
		static private var listSecuredDomains:Array = new Array();
		
		static public function mergeArray(source:Array, target:*):void
		{
			for each (var item:* in source)
			{
				if (target is ArrayCollection)
				{
					target.addItem(item);
				}
				else if (target is Array)
				{
					target.push(item);
				}
			}
		}
		
		static public function getObjectProperties(value:Object):Array
		{	
			var result:Array = new Array();		
			var buffer:XML = describeType(value);
			
			for each (var item:XML in buffer.variable)
			{
				result.push((item.@name).toString());
			}				
							
			return result;
		}
		
		static public function secureUrl(url:String):void
		{
			if (url && url.length > 10)
			{
				var domain:String = url.substr(0,url.indexOf('/',10));
				
				if (listSecuredDomains.indexOf(domain) == -1)
				{
					listSecuredDomains.push(domain);
					
					Security.allowDomain(domain);
					Security.loadPolicyFile(domain + '/crossdomain.xml');
				}
			}
		}
		
		static public function copyArray (copy:Array,paste:*):void
		{
			var item:*;
			
			if (paste is Array)
			{
				for each (item in copy)
				{
					paste.push(item);
				}
			}
			else if (paste is ArrayCollection)
			{
				paste.removeAll();
				
				for each (item in copy)
				{
					paste.addItem(item);
				}
			}
		} 
		
		static public function formatLink (url:String):String
		{
			if (url == null)
			{
				return null;	
			}
			
			var a:int = url.indexOf("/",10);
			
			if (a == -1)
			{
				return url;
			}
			else
			{
				return url.substr(a + 1);
			}
		}
		
		static public function getGlobalPosition(item:UIComponent):Point
		{
			var pt_0:Point = new Point(item.x,item.y);
			
			if (item.parent && item.parent is Container)
			{				
				pt_0 = (item.parent as Container).contentToGlobal(pt_0);
			}		
				
			return pt_0;
		}
		
		static public function cropString (value:String,l:uint):String
		{
			return value.length<l?value:(value.substr(0,l) + '..');
		}
		
		static public function upperFirst (value:String):String
		{
			if (value == null) return null;
			
			var f:String = value.substr(0,1).toUpperCase();
			
			return f + value.substr(1).replace(new RegExp('_','gi'),' ');
		}
		
		static public function formatNumberZero (value:Number):String
		{
			if (value < 10)
			{
				return '0' + value;
			}
			else
			{
				return value.toString();
			}
		}
		
		static public function sqlServerStringToDate (value:String):Date
		{
			var y:String = value.substr(0,4);
			var m:String = value.substr(5,2);
			var d:String = value.substr(8,2);
			
			return new Date(parseInt(y),parseInt(m)-1,parseInt(d));
		}
		
		static public const listMonths:Array = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		 
		
		static public function formatDate (d:Date):String
		{
			if (d == null)
			{
				return "";
			}
			else
			{
				var buffer:String = formatNumberZero(d.date) + ' ' + listMonths[d.month].toString().substr(0,3) + ' ' + d.fullYear.toString().substr(2,2);
				
				if (d.hours > 0)
				{
					buffer = d.hours + ':' + formatNumberZero(d.minutes) + ' ' + buffer;
				}
				
				return buffer;
			}
		}
		
		static public function formatNumber (value:Number):String
		{
			var ff:NumberFormatter = new NumberFormatter();
			
			ff.useThousandsSeparator = true;
					
			return ff.format(value);
		}
		
		static public function toSqlServerDate(d:Date):String
		{
			return d.fullYear + '-' + EbuildyUtils.formatNumberZero(d.month+1) + '-' + EbuildyUtils.formatNumberZero(d.date);
		}		
		
		static public function xmlFormat (value:String):String
		{
			value = value.replace("'","");
			
			return value;	
		}
		
		static public function formatTimeFromSeconds(value:Number):String
		{
			value = Math.round(value);
			
			return Math.floor(value / 60) + ":" + formatNumberZero(value % 60);
		}
		
		static public function formatFilesize(value:uint):String
		{
			if (value > 1000*1024*1024)
			{
				return (value/(1000*1024*1024)).toFixed(2) + " GB";
			}
			if (value > 100*1024)
			{
				return (value/(1024*1024)).toFixed(2) + " MB";
			}
			else if (value > 1024)
			{
				return (value/1024).toFixed(2) + " KB";		
			}
			else
			{
				return value + " B";
			}
		}
			
		static public function arrayRemove(array:Array,value:Object):Boolean
		{
			if (array)
			{
				var i:int = array.indexOf(value);
				
				if (i != -1)
				{
					array.splice(i,1);
					
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		static public function getItemByField(source:Object,field:String,value:Object):Object
		{
			for each (var item:Object in source)
			{
				if (item.hasOwnProperty(field) && item[field] == value)
				{
					return item;
				}
			}
			
			return null;
		}
		
		static public function showConfirm(text:String,title:String,handler:Function):void
		{
			Alert.show(text,title,Alert.YES | Alert.NO,null,function a(e:CloseEvent):void
			{
				if (e.detail == Alert.YES)
				{
					handler();
				}
			});
		}
	}
}